package com.ego.receive;

import com.ego.commons.pojo.BigAd;
import com.ego.commons.pojo.TbItemDetails;
import com.ego.commons.utils.HttpClientUtil;
import com.ego.commons.utils.JsonUtils;
import com.ego.dubbo.service.TbContentDubboService;
import com.ego.dubbo.service.TbItemDubboService;
import com.ego.pojo.TbContent;
import com.ego.pojo.TbItem;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ghp
 */
@Component
public class QueueListener {
    @Reference
    private TbContentDubboService tbContentDubboService;

    @Value("89")
    private Long bigadId;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Value("${ego.search.insert}")
    private String searchInsertUrl;
    @Value("${ego.search.delete}")
    private String searchDeleteUrl;

    @Reference
    private TbItemDubboService tbItemDubboService;

    // 最简单的写法，但是如果当没有发送消息时，队列没有创建，启动当前项目就会报错。
    //  @RabbitListener(queues = "content")
    // 即使没有队列，也可以启动receive，自动帮助创建对垒

    /**
     *
     * @param obj
     */
    @RabbitListener(bindings = {@QueueBinding(value = @Queue(name = "content"), exchange = @Exchange(name = "amq.direct"))})
    public void content(Object obj) {
        System.out.println("接收到消息：" + obj);
        /*
            redis数据同步
            1. 从mysql中取出大广告的数据
               做成Dubbo的Consumer
            2. 把数据缓存到redis中。
         */
        // 实现方案1 。把portal的consumer代码复制过来。
        List<TbContent> list = tbContentDubboService.selectAllByCategoryId(bigadId);
        List<BigAd> listBigad = new ArrayList<>();
        for (TbContent tbContent : list) {
            BigAd bigAd = new BigAd();
            bigAd.setAlt("");
            bigAd.setHeight(240);
            bigAd.setHeightB(240);
            bigAd.setHref(tbContent.getUrl());
            bigAd.setSrc(tbContent.getPic());
            bigAd.setSrcB(tbContent.getPic2());
            bigAd.setWidth(670);
            bigAd.setWidthB(550);
            listBigad.add(bigAd);
        }
        redisTemplate.opsForValue().set("com.ego.portal::bigad", JsonUtils.objectToJson(listBigad));
        // 第二种方式
        // 直接调用ego_portal
        //HttpClientUtil.doGet("http://localhost:8082/bigad");
    }

    @RabbitListener(bindings = {@QueueBinding(value = @Queue(name = "${ego.rabbitmq.item.insertName}"), exchange = @Exchange(name = "amq.direct"))})
    public void itemInsert(String id) {
        //solr同步
        System.out.println("获取到的id:" + id);
        Map<String, String> param = new HashMap<>();
        param.put("id", id + "");
        String result = HttpClientUtil.doPost(searchInsertUrl, param);
        System.out.println("返回的结果："+result);

        //对redis数据同步，此方式对ego_item性能无影响
        String[] ids =id.split(",");
        for (String idArr : ids){
            String key = "com.ego.item::details:"+idArr;
            TbItem tbItem = this.tbItemDubboService.selectById(Long.parseLong(idArr));
            TbItemDetails details = new TbItemDetails();
            details.setId(tbItem.getId());
            details.setPrice(tbItem.getPrice());
            details.setSellPoint(tbItem.getSellPoint());
            details.setTitle(tbItem.getTitle());
            String img = tbItem.getImage();
            details.setImages(img!=null&&!img.equals("")?img.split(","):new String[1]);
            redisTemplate.opsForValue().set(key,details);
        }
    }

    @RabbitListener(bindings = {@QueueBinding(value = @Queue(name = "${ego.rabbitmq.item.deleteName}"), exchange = @Exchange(name = "amq.direct"))})
    public void itemDelete(String id) {
        System.out.println("delete获取到的id:" + id);
        Map<String, String> param = new HashMap<>();
        param.put("id", id + "");
        String result = HttpClientUtil.doPost(searchDeleteUrl, param);
        System.out.println("返回的结果："+result);
    }
}

